home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CD32 / CD32_Support / examples / SA_Examples / lowlevel / ReadJoyPort / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-31  |  6.0 KB  |  307 lines

  1. /****** CDGS/ReadJoyPort **********************************************
  2. *
  3. *   NAME
  4. *       ReadJoyPort -- test program for lowlevel.library/ReadJoyPort()
  5. *
  6. *   SYNOPSIS
  7. *       ReadJoyPort PORT/N,STICKY/S,DEBUG/S
  8. *
  9. *   FUNCTION
  10. *       Test program for lowlevel.library/ReadJoyPort().
  11. *
  12. *       Continually displays controller type and status for
  13. *       specified port.
  14. *
  15. *       To terminate this program, select the close gadget.
  16. *
  17. *   INPUTS
  18. *       PORT                -   Port number (default is 0)
  19. *       STICKY              -   Sticky (default is off)
  20. *       DEBUG               -   debug mode
  21. *
  22. *   RESULT
  23. *       RETURN_OK (0)       -   success
  24. *       RETURN_WARN (5)     -   warning
  25. *       RETURN_ERROR (10)   -   error
  26. *       RETURN_FAIL (20)    -   failure
  27. *
  28. *   EXAMPLE
  29. *
  30. *   NOTES
  31. *
  32. *   BUGS
  33. *       Hopefully very few.
  34. *
  35. *   SEE ALSO
  36. *
  37. ******************************************************************************
  38. *
  39. */
  40.  
  41. /*
  42.  * System includes
  43.  */
  44.  
  45. #include <exec/types.h>
  46. #include <exec/memory.h>
  47.  
  48. #include <dos/dos.h>
  49.  
  50. #include <intuition/intuition.h>
  51. #include <intuition/gadgetclass.h>
  52.  
  53. #include <libraries/gadtools.h>
  54.  
  55. #include <libraries/lowlevel.h>
  56.  
  57. #include <clib/exec_protos.h>
  58. #include <clib/dos_protos.h>
  59. #include <clib/graphics_protos.h>
  60. #include <clib/intuition_protos.h>
  61. #include <clib/gadtools_protos.h>
  62. #include <clib/lowlevel_protos.h>
  63.  
  64. #include <pragmas/lowlevel_pragmas.h>
  65.  
  66. /*
  67.  * ANSI includes
  68.  */
  69.  
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <string.h>
  73.  
  74. /*
  75.  * Local includes
  76.  */
  77.  
  78. #define MAIN
  79. #include "readjoyport.h"
  80.  
  81. #include <local/WBCliArgs.h>
  82. #include <local/calib_protos.h>
  83. #include <local/mwlib_protos.h>
  84.  
  85. /****** ReadJoyPort/main ******************************************
  86. *
  87. *   NAME
  88. *       main -- main entry point
  89. *
  90. *   SYNOPSIS
  91. *       main(argc,argv);
  92. *
  93. *       void main(int argc,char *argv[]);
  94. *
  95. *   FUNCTION
  96. *       Main entry point.
  97. *
  98. *   INPUTS
  99. *       argc -- argument count
  100. *       argv -- argument value arrraay
  101. *
  102. *   RESULT
  103. *       None
  104. *
  105. *   EXAMPLE
  106. *
  107. *   NOTES
  108. *
  109. *   BUGS
  110. *       If there are any, you can get to 'em from here!
  111. *
  112. *   SEE ALSO
  113. *       shutdown()
  114. *
  115. ******************************************************************************
  116. *
  117. */
  118. void main(int argc,char *argv[])
  119. {
  120.  
  121.     struct options {
  122.         LONG *port;
  123.         LONG sticky;
  124.         LONG debug;
  125.     } options;
  126.  
  127.     /*
  128.      * Parse arguments
  129.      */
  130.  
  131.     /* Parse arguments from Workbench, CLI, or GUI */
  132.     memset(&options,0,sizeof(options));
  133.     parseData=WBCliArgsTags(
  134.         WC_Template, "PORT/N,STICKY/S,DEBUG/S",
  135.         WC_Args, &options,
  136.         WC_Startup, argv,
  137.         WC_TrustStartup, !argc,
  138.         WC_Project, TRUE);
  139.     if (options.port) {
  140.         portNumber=*options.port;
  141.     }
  142.     sticky=options.sticky;
  143.     debugMode=options.debug;
  144.  
  145.     /*
  146.      * Open libraries
  147.      */
  148.  
  149.     /* Open graphics.library */
  150.     GfxBase=OpenLibrary("graphics.library",KICKSTART_VERSION);
  151.     if (!GfxBase) {
  152.         Printf("%s: Error opening graphics.library V%ld\n",
  153.             PROGRAM_NAME,KICKSTART_VERSION);
  154.         goodbye(RETURN_FAIL);
  155.     }
  156.     if (debugMode) {
  157.         Printf("ReadJoyPort/main: graphics.library open\n");
  158.     }
  159.  
  160.     /* Open intuition.library */
  161.     IntuitionBase=OpenLibrary("intuition.library",KICKSTART_VERSION);
  162.     if (!IntuitionBase) {
  163.         Printf("%s: Error opening intuition.library V%ld\n",
  164.             PROGRAM_NAME,KICKSTART_VERSION);
  165.         goodbye(RETURN_FAIL);
  166.     }
  167.     if (debugMode) {
  168.         Printf("ReadJoyPort/main: intuition.library open\n");
  169.     }
  170.  
  171.     /* Open gadtools.library */
  172.     GadtoolsBase=OpenLibrary("gadtools.library",KICKSTART_VERSION);
  173.     if (!GadtoolsBase) {
  174.         Printf("%s: Error opening gadtools.library V%ld\n",
  175.             PROGRAM_NAME,KICKSTART_VERSION);
  176.         goodbye(RETURN_FAIL);
  177.     }
  178.     if (debugMode) {
  179.         Printf("ReadJoyPort/main: gadtools.library open\n");
  180.     }
  181.  
  182.     /* Open lowlevel.library */
  183.     LowLevelBase=OpenLibrary("lowlevel.library",KICKSTART_VERSION);
  184.     if (!LowLevelBase) {
  185.         Printf("%s: Error opening lowlevel.library V%ld\n",
  186.             PROGRAM_NAME,KICKSTART_VERSION);
  187.         goodbye(RETURN_FAIL);
  188.     }
  189.     if (debugMode) {
  190.         Printf("ReadJoyPort/main: lowlevel.library open\n");
  191.     }
  192.  
  193.     /*
  194.      * GUI
  195.      */
  196.  
  197.     /* Open GUI */
  198.     if (!guiOpen()) {
  199.         Printf("%s: Error opening user interface\n",PROGRAM_NAME);
  200.         goodbye(RETURN_FAIL);
  201.     }
  202.     if (debugMode) {
  203.         Printf("ReadJoyPort/main: GUI good\n");
  204.     }
  205.  
  206.     /* Process GUI */
  207.     guiLoop();
  208.     if (debugMode) {
  209.         Printf("ReadJoyPort/main: Event loop complete\n");
  210.     }
  211.  
  212.     /*
  213.      * Fall-through
  214.      */
  215.  
  216.     if (debugMode) {
  217.         Printf("ReadJoyPort/main: Debug mode\n");
  218.     }
  219.     goodbye(RETURN_OK);
  220.  
  221. }
  222.  
  223. /****** ReadJoyPort/goodbye ******************************************
  224. *
  225. *   NAME
  226. *       goodbye -- terminate program
  227. *
  228. *   SYNOPSIS
  229. *       goodbye(returnCode);
  230. *
  231. *       void goodbye(int returnCode);
  232. *
  233. *   FUNCTION
  234. *       Terminate program.
  235. *
  236. *   INPUTS
  237. *       returnCode -- return code (from dos/dos.h RETURN_*)
  238. *
  239. *   RESULT
  240. *       None
  241. *
  242. *   EXAMPLE
  243. *
  244. *   NOTES
  245. *
  246. *   BUGS
  247. *
  248. *   SEE ALSO
  249. *       main()
  250. *
  251. ******************************************************************************
  252. *
  253. */
  254. void goodbye(int returnCode)
  255. {
  256.  
  257.     /*
  258.      * Close GUI
  259.      */
  260.  
  261.     /* Close GUI */
  262.     guiClose();
  263.  
  264.     /*
  265.      * Close libraries
  266.      */
  267.  
  268.     /* Close lowlevel.library */
  269.     if (LowLevelBase) {
  270.         /* Reset ports 0 and 1 to autosense before closing */
  271.         SetJoyPortAttrs(0,
  272.             SJA_Type, SJA_TYPE_AUTOSENSE,
  273.             TAG_DONE);
  274.         SetJoyPortAttrs(1,
  275.             SJA_Type, SJA_TYPE_AUTOSENSE,
  276.             TAG_DONE);
  277.         CloseLibrary(LowLevelBase);
  278.     }
  279.  
  280.     /* Close gadtools.library */
  281.     if (GadtoolsBase) {
  282.         CloseLibrary(GadtoolsBase);
  283.     }
  284.  
  285.     /* Close intuition.library */
  286.     if (IntuitionBase) {
  287.         CloseLibrary(IntuitionBase);
  288.     }
  289.  
  290.     /*
  291.      * Close command-line parsing
  292.      */
  293.  
  294.     /* Close WBCliArgs() control structure */
  295.     if (parseData) {
  296.         FreeWBCli(parseData);
  297.     }
  298.  
  299.     /*
  300.      * Exit
  301.      */
  302.  
  303.     /* Exit */
  304.     exit(returnCode);
  305.  
  306. }
  307.